# +----------------------------------------------------------------------
# | Pink [ A modern python web framework ]
# +----------------------------------------------------------------------
# | Copyright (c) 2023 http:#unnnnn.com All rights reserved.
# +----------------------------------------------------------------------
# | Author: chenjianhua <unnnnn@foxmail.com>
# +----------------------------------------------------------------------

"""Queue Failed Command."""
import os

from .Command import Command
from ..utils.filesystem import make_directory, get_module_dir
from ..utils.location import base_path
from ..utils.time import migration_timestamp
from cleo.io.inputs.option import Option as option


class QueueFailedCommand(Command):
    name = 'queue:failed'
    description = 'Creates a failed jobs table'
    options = [
        option('directory', 'd',
               description='The directory where the migration file will be created.'
               )
    ]

    """
    Creates a failed jobs table

    queue:failed
        {--d|--directory=databases/migrations : Specifies the directory to create the migration in}
    """

    def handle(self):
        with open(
                os.path.join(
                    get_module_dir(__file__), "../stubs/queue/create_failed_jobs_table.py"
                )
        ) as fp:
            output = fp.read()

        filename = f"{migration_timestamp()}_create_failed_jobs_table.py"
        path = os.path.join(base_path(self.option("directory")), filename)
        make_directory(path)

        with open(path, "w") as fp:
            fp.write(output)

        self.info(f"Migration file created: {filename}")
